//
//  STTools_RcimUserCacheManger.m
//  WithHeart
//
//  Created by imac on 2019/12/30.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "STTools_RcimUserCacheManger.h"
#define TMRCIM_USER_CACHE_KEY @"TMRCIM_USER_CACHE_KEY"
#define TMRCIM_GROUP_CACHE_KEY @"TMRCIM_GROUP_CACHE_KEY"
@interface STTools_RcimUserCacheManger()

@end
@implementation STTools_RcimUserCacheManger
+ (STTools_RcimUserCacheManger *)manger{
    static STTools_RcimUserCacheManger * manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = [STTools_RcimUserCacheManger new];
    });
    return manger;
}
//用户
- (NSArray<RCUserInfo*>*)getAllUserArray{
    NSArray * array = [NSUserDefaults.standardUserDefaults valueForKey:TMRCIM_USER_CACHE_KEY];
    NSArray *dataSouce = [RCUserInfo mj_objectArrayWithKeyValuesArray:array];
    return  dataSouce;
}
- (RCUserInfo*)getUserInfoFormUserId:(NSString*)userId{
    NSArray * array = [self getAllUserArray];
    for (RCUserInfo * info in array) {
        if ([info.userId isEqualToString:userId]) {
            return info;
        }
    }
    return nil;
}
- (void)saveUserInfoIfNeed:(RCUserInfo*)info{
    NSArray * array = [self getAllUserArray];
    BOOL exist = NO;
    for (RCUserInfo * existInfo in array) {
        if ([existInfo.userId isEqualToString:info.userId]) {
            exist = YES;
        }
    }
    if (!exist) {
        NSArray * array = [NSUserDefaults.standardUserDefaults valueForKey:TMRCIM_USER_CACHE_KEY];
        NSMutableArray * dealArray = array.mutableCopy;
        if (!dealArray) {
            dealArray = NSMutableArray.new;
        }
        [dealArray addObject:info.mj_JSONString];
        [NSUserDefaults.standardUserDefaults setValue:dealArray.copy forKey:TMRCIM_USER_CACHE_KEY];
        [NSUserDefaults.standardUserDefaults synchronize];
    }
}

//群组
- (NSArray<RCGroup*>*)getAllGroupArray{
    NSArray * array = [NSUserDefaults.standardUserDefaults valueForKey:TMRCIM_GROUP_CACHE_KEY];
    NSArray *dataSouce = [RCUserInfo mj_objectArrayWithKeyValuesArray:array];
    return  dataSouce;
}
- (RCGroup*)getGroupInfoFormGroupId:(NSString*)groupId{
    NSArray * array = [self getAllGroupArray];
    for (RCGroup * info in array) {
        if ([info.groupId isEqualToString:groupId]) {
            return info;
        }
    }
    return nil;
}
- (void)saveGroupInfoIfNeed:(RCGroup*)info{
    NSArray * array = [self getAllGroupArray];
    BOOL exist = NO;
    for (RCGroup * existGroup in array) {
        if ([existGroup.groupId isEqualToString:info.groupId]) {
            exist = YES;
        }
    }
    if (!exist) {
        NSArray * array = [NSUserDefaults.standardUserDefaults valueForKey:TMRCIM_GROUP_CACHE_KEY];
        NSMutableArray * dealArray = array.mutableCopy;
        if (!dealArray) {
            dealArray = NSMutableArray.new;
        }
        [dealArray addObject:info.mj_JSONString];
        [NSUserDefaults.standardUserDefaults setValue:dealArray.copy forKey:TMRCIM_GROUP_CACHE_KEY];
        [NSUserDefaults.standardUserDefaults synchronize];
    }
}
@end
